import { AsyncStorage } from 'react-native';
import Request from "../utils/Request";
import constant from "../utils/constant";
import { default as RouterServer } from "../utils/RouterServer";
import PushHandle from "../push.ios";
const { Actions } = require("react-native-router-flux");
const { Toast } = require('antd-mobile');
const STORAGE_KEY = "STORAGE_KEY_auth";
export default {
    namespace: 'auth',
    state: {
        isSignIn: false,
        sendButtonText: '发送验证码',
        sending: false,
        loading: false,
        coldDownTime: 10,
        profile: null
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        *saveToStorage({ payload }, { call }) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload));
        },
        *restoreFromStorage({ payload }, { call, put, select }) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            yield put({
                type: 'save',
                payload: Object.assign({}, JSON.parse(data), { loading: false })
            });
            const auth = yield select(state => state.auth);
            if (auth.isSignIn) {
                PushHandle.setAlias(auth.profile.id);
                if (auth.profile.active === 0) {
                    yield call(RouterServer.push, 'createShop', { title: '创建店铺' });
                }
                else {
                    yield call(RouterServer.replace, 'tabbar', { title: '创建店铺' });
                }
            }
            else {
                PushHandle.setAlias('');
            }
        },
        *clearTimer({ payload }, { put, select }) {
            yield put({
                type: 'save',
                payload: { sending: false, loading: false, sendButtonText: '获取验证码', coldDownTime: 10 }
            });
        },
        *sendSms({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: { sending: true }
            });
            const data = yield call(Request.post, Request.getApi().sendSms, Object.assign({}, payload));
            if (data) {
                Toast.success('验证码已发送请注意查收', constant.ToastDuration);
            }
            else {
                yield put({
                    type: 'clearTimer'
                });
            }
        },
        *signUp({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            const data = yield call(Request.post, Request.getApi().signUp, Object.assign({}, payload));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                yield call(Actions.pop);
            }
            else {
            }
        },
        *signIn({ payload }, { call, put, select }) {
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            const data = yield call(Request.post, Request.getApi().signIn, Object.assign({}, payload));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                yield put({
                    type: 'save',
                    payload: {
                        profile: Object.assign({}, data.data.business),
                        isSignIn: true
                    }
                });
                const auth = yield select(state => state.auth);
                yield put({
                    type: 'saveToStorage',
                    payload: Object.assign({}, auth, { loading: false })
                });
                if (data.data.business.active === 1) {
                    yield call(RouterServer.replace, 'tabbar');
                    PushHandle.setAlias(auth.profile.id);
                }
                else {
                    yield call(RouterServer.push, 'createShop', { title: '创建店铺' });
                }
            }
            else {
                yield put({ type: 'logOut' });
            }
        },
        *createStore({ payload }, { call, put, select }) {
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            const data = yield call(Request.post, Request.getApi().createShop, Object.assign({}, payload));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                yield put({
                    type: 'save',
                    payload: {
                        profile: Object.assign({}, data.business),
                        isSignIn: false
                    }
                });
                const auth = yield select(state => state.auth);
                yield put({
                    type: 'saveToStorage',
                    payload: Object.assign({}, auth, { loading: false })
                });
                if (data.data.active === 1) {
                    yield call(RouterServer.replace, 'tabbar');
                }
                else {
                    yield call(RouterServer.push, 'createShop', { title: '创建店铺' });
                }
            }
            else {
                console.log('error', data);
            }
        },
        *editStore({ payload }, { call, put, select }) {
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            const auth = yield select(state => state.auth);
            const data = yield call(Request.post, Request.getApi().changeShopInfo, Object.assign({ store_phone: auth.profile.store_phone, store_id: auth.profile.id }, payload));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                auth.profile = data.data;
                yield put({
                    type: 'save',
                    payload: Object.assign({}, auth)
                });
                yield put({
                    type: 'saveToStorage',
                    payload: Object.assign({}, auth, { loading: false })
                });
            }
            else {
                console.log('error', data);
            }
        },
        *logOut({ payload }, { call, put, select }) {
            yield call(RouterServer.replace, 'auth');
            yield put({
                type: 'save',
                payload: {
                    isSignIn: false,
                    profile: null
                }
            });
            const auth = yield select(state => state.auth);
            yield put({
                type: 'saveToStorage',
                payload: Object.assign({}, auth)
            });
        },
        *changePassword({ payload }, { call, put, select }) {
            yield put({
                type: 'save',
                payload: {
                    loading: true,
                }
            });
            const data = yield call(Request.post, Request.getApi().changePwd, payload);
            yield put({
                type: 'save',
                payload: {
                    loading: false
                }
            });
            if (data) {
                Toast.success('修改成功!', constant.ToastDuration);
                yield call(RouterServer.replace, 'auth');
                yield put({
                    type: 'save',
                    payload: {
                        loading: false,
                        isSignIn: false,
                        profile: null
                    }
                });
            }
        },
        *upDataBalance({ payload }, { call, put, select }) {
            yield put({
                type: 'save',
                payload: {
                    loading: true,
                }
            });
            const auth = yield select(state => state.auth);
            const data = yield call(Request.get, Request.getApi().totalMoney, Object.assign({ store_id: auth.profile.id }, payload));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                auth.profile.store_collect = data.data;
                yield put({
                    type: 'save',
                    payload: Object.assign({}, auth, { loading: false })
                });
                yield put({
                    type: 'saveToStorage',
                    payload: Object.assign({}, auth, { loading: false })
                });
            }
        },
        *doWithDraw({ payload }, { call, put, select }) {
            yield put({
                type: 'save',
                payload: {
                    loading: false,
                }
            });
            const auth = yield select(state => state.auth);
            const data = yield call(Request.post, Request.getApi().withdrawApply, Object.assign({ store_id: auth.profile.id }, payload));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                Toast.success('提现申请已经提交', constant.ToastDuration);
                yield put({
                    type: 'upDataBalance'
                });
            }
        },
    },
    subscriptions: {
        setup({ dispatch }) {
            dispatch({
                type: 'restoreFromStorage'
            });
        }
    },
};
